/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.view.saving;

import java.io.Serializable;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.context.RequestContext;
import org.primefaces.event.CloseEvent;
import src.domain.saving.Savfeature;
import src.services.saving.SavfeatureModel;
import src.util.Messager;

/**
 *
 * @author jay
 */
@ManagedBean
@SessionScoped
public class SavfeatureBean implements Serializable {

    private List<Savfeature> savfeatures;
    private Savfeature selectedData, unsaveData;
    private SavfeatureModel helper;
    private boolean editMode = true;

    public SavfeatureBean() {
        this.helper = new SavfeatureModel();
        this.savfeatures = helper.findAll(null);
        this.selectedData = null;
        this.unsaveData = null;
    }

    public boolean isEditMode() {
        return editMode;
    }
    
    public List<Savfeature> getSavfeatures() {
        return this.savfeatures;
    }
    
    public Savfeature getSelectedData() {
        return selectedData;
    }

    public void setSelectedData(Savfeature selectedData) {
        this.selectedData = selectedData;
    }
    
    public Savfeature getInsertedData() {
        return unsaveData;
    }

    public Savfeature getUnsaveData() {
        return unsaveData;
    }

    public void setUnsaveData(Savfeature unsaveData) {
        this.unsaveData = unsaveData;
    }
    
    public void handleClose(CloseEvent event) {
        this.selectedData = null;
        this.unsaveData = null;
        this.editMode = true;      
    }
    
    public void openData() { 
        this.editMode = true;
        this.unsaveData = Savfeature.clone(selectedData);
    }
    
    public void newData() {  
        this.selectedData = null;
        this.editMode = false;
        this.unsaveData = new Savfeature();
    }
    
    public void save() {
        
        RequestContext context = RequestContext.getCurrentInstance();  
        boolean success = helper.saveData(unsaveData);
        if (success==true) {
            if (this.editMode==false) this.savfeatures.add(unsaveData);
            else {
                int i = savfeatures.indexOf(selectedData);
                if (i <= savfeatures.size()) savfeatures.set(i, Savfeature.clone(unsaveData));
            }
            editMode = true;
            
        } else {
            if (this.editMode==false) unsaveData.setSavfeatureId(null);
        } 
        
        if(!success) Messager.showSaveUpdateStatus(success);
        context.addCallbackParam("success", success);  
    }
    
    public void delete() {
        RequestContext context = RequestContext.getCurrentInstance();  

        boolean success = helper.deleteData(this.selectedData);
        if (success) {
            this.savfeatures.remove(this.selectedData);
            this.selectedData = null;
        }
               
        if(!success) Messager.showDeleteStatusConstaint(success);
        context.addCallbackParam("success", success);  
    }
    
}
